package modes

import (
	"fmt"
	"time"
)

/*
 * 描述: 章节表
 *
 *
 *****************************/

type SubjectQues struct {
	Id     int64  `json:"s_id" xorm:"id"`         //章节ID
	PId    int64  `json:"s_pid" xorm:"pid"`       //项目ID
	Sid    int64  `json:"sid" xorm:"sid"`         //科目ID
	Name   string `json:"name" xorm:"name"`       //科目名称
	Start  int    `json:"s_start" xorm:"start"`   //状态 0启用 1禁用
	At     int64  `json:"s_at" xorm:"at"`         //时间
	THasdo int64  `json:"t_hasdo" xorm:"t_hasdo"` //已做人数

	Sort int `json:"k_sort" xorm:"sort"` //排序 倒叙
}

func (this *SubjectQues) TableName() string {
	return "subject_ques"
}

func (this *SubjectQues) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(0).Insert(this)
}

func (this *SubjectQues) Get() (bool, error) {
	return Db(0).Get(this)
}
func (this *SubjectQues) AddHasDo() {
	this.update(fmt.Sprintf(" id=%d ", this.Id), "t_hasdo")
}
func (this *SubjectQues) IdDel() (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return Db(0).Where(where).Delete(this)
}

func (this *SubjectQues) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *SubjectQues) IdSet(field string) (int64, error) {
	where := fmt.Sprintf(" id = %d  ", this.Id)
	return this.update(where, field)
}

func (this *SubjectQues) Count(where string) (int64, error) {
	var val SubjectQues
	return Db(0).Where(where).Count(&val)
}
func (this *SubjectQues) GetList() ([]SubjectQues, error) {
	list := make([]SubjectQues, 0)
	err := Db(0).Find(&list, &this)
	return list, err
}

func (this *SubjectQues) where(fage, count, page int, where, field string) ([]SubjectQues, error) {
	list := make([]SubjectQues, 0)
	var err error
	page--
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}

func (this *SubjectQues) GetName(kid []int64) ([]SubjectQues, error) {
	list := make([]SubjectQues, 0)
	err := Db(1).Where("start = 0").In("id", kid).Find(&list)
	return list, err
}

func (this *SubjectQues) GetPageList(page *PageFields) (int64, []SubjectQues, error) {
	var subjectQuesList []SubjectQues
	where := fmt.Sprintf("sid=%d ", this.Sid)

	tbl := Db(0).Table("subject_ques").Where(where)

	total, err := tbl.Clone().Count()
	if err != nil {
		return 0, nil, err
	}
	err = tbl.Desc("at").Limit(page.Pnum, (page.Page-1)*page.Pnum).Find(&subjectQuesList)

	if err != nil {
		return 0, nil, err
	}
	return total, subjectQuesList, nil
}

func (this *SubjectQues) GetListBySid() ([]SubjectQues, error) {
	var subjectQuesList []SubjectQues
	where := fmt.Sprintf("sid=%d and start=0 ", this.Sid)

	tbl := Db(0).Table("subject_ques").Where(where)

	err := tbl.Desc("sort").Find(&subjectQuesList)
	return subjectQuesList, err
}

func (this *SubjectQues) GetListByIds(sqidList []int64) ([]SubjectQues, error) {

	subjectQuesList := make([]SubjectQues, 0)
	err := Db(0).In("id", sqidList).Find(&subjectQuesList)
	return subjectQuesList, err
}

func (this *SubjectQues) GetListBySId() ([]SubjectQues, error) {
	subjectQuesList := make([]SubjectQues, 0)
	err := Db(0).Where("sid=?", this.Sid).Find(&subjectQuesList)
	return subjectQuesList, err

}
